package com.cnzz.site1255174697.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.ali.auth.third.core.config.ConfigManager;
import com.ali.auth.third.core.util.ResourceUtils;
import com.ali.auth.third.login.RequestCode;
import com.ali.auth.third.ui.LoginWebViewActivity;
import com.ali.auth.third.ui.webview.BaseWebViewClient;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.activity.WebViewActivity;

import java.io.IOException;

public class LoginActivity
        extends LoginWebViewActivity {
    WebViewClient webViewClient;
    private FrameLayout mFrameLayout;
    View reg_layout;
    EditText et_username;
    EditText et_password;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        mFrameLayout = (FrameLayout) findViewById(android.R.id.content);
        webViewClient = this.createWebViewClient();
        taeWebView.setWebViewClient(new BaseWebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                WebResourceResponse response = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    response = super.shouldInterceptRequest(view, url);
                    if (url.contains("mlogin.css")) {
                        try {
                            response = new WebResourceResponse("text/css", "UTF-8", getAssets().open("mlogin.css"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (url.contains("newauthlogin.js")) {
                        try {
                            response = new WebResourceResponse("text/javascript", "UTF-8", getAssets().open("newauthlogin.js"));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return response;
            }

            public boolean shouldOverrideUrlLoading(WebView var1, String var2) {
                return webViewClient.shouldOverrideUrlLoading(var1, var2);
            }
            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
                reg_layout.setVisibility(View.VISIBLE);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                titleText.setText("登录");

            }
        });
        titleText.setText("登录");
        reg_layout = LayoutInflater.from(this).inflate(R.layout.reg_layout, null);
        mFrameLayout.addView(reg_layout);
        et_username = (EditText) reg_layout.findViewById(R.id.et_username);
        et_password = (EditText) reg_layout.findViewById(R.id.et_password);
    }

    public void onBtnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_reg:
                startActivity(new Intent(this, WebViewActivity.class).putExtra("url", "https://reg.taobao.com/member/reg/h5/fill_mobile.htm").putExtra("title", "免费注册"));
                break;
            case R.id.btn_findpassword:
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://h5.m.taobao.com/index_error.html?from=h5error");
                intent.setData(content_url);
                startActivity(intent);

//                DialogPopupUtils.showTaobaoDialog(this);
                break;
//            case R.id.btn_login:
//                taeWebView.loadUrl("javascript:toLongin(\"" + et_username.getText().toString() + "\",\"" + et_password.getText().toString() + "\")");
////                taeWebView.loadUrl("javascript:funFromjs()");
//                break;
        }
    }

    public static void startActivity(Activity context) {
        Intent var2 = new Intent(context, LoginActivity.class);
        LoginWebViewActivity.originActivity = context;
        var2.putExtra("url", ConfigManager.LOGIN_HOST);
        var2.putExtra("title", ResourceUtils.getString("com_taobao_tae_sdk_authorize_title"));
        context.startActivityForResult(var2, RequestCode.OPEN_H5_LOGIN);
    }
}
